package org.wjy.post.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.wjy.post.dao.UserDao;
import org.wjy.post.other.Json;
import org.wjy.post.service.GoodsService;
import org.wjy.post.service.OrdersService;
import org.wjy.post.service.Sale_listService;
import org.wjy.post.vo.Goods;
import org.wjy.post.vo.Orders;
import org.wjy.post.vo.Sale_list;
import org.wjy.post.vo.User;


import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;

/**     
 * 类名称：LoginAction   
 * 类描述：用户控制器的实现
 * 创建人：anan   
 * 创建时间：2012-12-21 下午11:17:36   
 * 修改人：anan  
 * 修改时间：2012-12-21 下午11:17:36   
 * 修改备注：   
 * @version        
 * */
@Namespace("/post")
public class SalesAction extends ActionSupport {
	private static final long serialVersionUID = -2266695172069461659L;
	private static final Logger logger = Logger.getLogger(SalesAction.class);
	@Resource(name="ordersService")
	private OrdersService ordersService;
	@Resource(name="sale_listService")
	private Sale_listService sale_listService;
	@Resource(name="goodsService")
	private GoodsService goodsService;
	private boolean flag;
	private int id;
	private String orderid;
	private String customerNo;
	private String customerName;
	private int goodsnum;
	private double payable;
	private int status;
	private String goodsid;
	private String cre_time;
	private int num;
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getOrderid() {
		return orderid;
	}

	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}

	public String getCustomerNo() {
		return customerNo;
	}

	public void setCustomerNo(String customerNo) {
		this.customerNo = customerNo;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public int getGoodsnum() {
		return goodsnum;
	}

	public void setGoodsnum(int goodsnum) {
		this.goodsnum = goodsnum;
	}

	public double getPayable() {
		return payable;
	}

	public void setPayable(double payable) {
		this.payable = payable;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(String status) {
		if(status.equals("已付款")){
			this.status=1;
		}
		else{
			this.status=0;
		}
		
	}
	
	public void setGoodsid(String goodsid) {
		this.goodsid = goodsid;
	}

	public String getGoodsid() {
		return goodsid;
	}
	
	public void setNum(int num) {
		this.num = num;
	}

	public int getNum() {
		return num;
	}
	public void setCre_time(String cre_time) {
		this.cre_time = cre_time;
	}

	public String getCre_time() {
		return cre_time;
	}
	
	public Orders getOrders(){
		Orders order=new Orders();
		order.setOrderid(orderid);
		order.setStatus(status);
		order.setPayable(payable);
		order.setId(id);
		order.setGoodsnum(goodsnum);
		order.setCustomerNo(customerNo);
		order.setCre_time(cre_time);
		return order;
		
	}

	public HttpSession getSession() {
		return getRequest().getSession();
	}
	
	public String getDate(){
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		return df.format(new Date());
	}
	
	@Action(value = "addorderAction", results = { })
	public void addorder(){
		Json ret=new Json();
		ret.setMsg("添加失败!!!");
		Orders order=new Orders();
		order.setCustomerNo(this.customerNo);
		order.setCre_time(this.getDate());
		flag=ordersService.addOrders(order);
		if(flag){
			ret.setSuccess(true);
			ret.setMsg("添加成功!!!");
		}
		String json = JSON.toJSONString(ret);
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Action(value = "addsalegoodsAction", results = { })
	public void addsalegoods(){
		Json ret=new Json();
		ret.setMsg("添加失败!!!");
		Sale_list sale_list=new Sale_list();
		sale_list.setGoodsid(goodsid);
		sale_list.setNum(num);
		sale_list.setOrderid(orderid);
		double price=goodsService.getprice(goodsid);
		System.out.print(price);
		flag=sale_listService.addSale_list(sale_list);
		
		if(flag){
			Orders order=getOrders();System.out.println(order.toString());
			order.setGoodsnum(order.getGoodsnum()+num);
			order.setPayable(order.getPayable()+price*num);
			ordersService.update(order);
			ret.setSuccess(true);
			ret.setMsg("添加成功!!!");
		}
		String json = JSON.toJSONString(ret);
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
}
